/* eslint-disable */
import {saveAs} from 'file-saver'
import ExcelJS from 'exceljs';
import dayjs from "dayjs";
import {DownloadExcelPrams, Product} from "@/interface/products";

// export const ExportFile = (data:any) => {
//     const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(data);
//     const workbook: XLSX.WorkBook = { Sheets: { 'mySheet': worksheet }, SheetNames: ['mySheet'] };
//     XLSX.writeFile(workbook, 'MyData.xlsx');
// }


// 文件导出
export function downloadExcel({ columns, rows, name = '未命名文件', company }: DownloadExcelPrams) {
    console.log(dayjs().format('YYYY-MM-DD HH时mm分'))
    const workbook = new ExcelJS.Workbook();
    workbook.creator = 'Start-front';
    workbook.lastModifiedBy = 'Start-front';
    workbook.created = new Date(1985, 8, 30);
    workbook.modified = new Date();
    workbook.lastPrinted = new Date(2016, 9, 27);

    console.log("公司名称：", company)
    // 将工作簿添加一个sheet页sheet1
    const sheet1 = workbook.addWorksheet(name, {
        // headerFooter: {firstHeader: company, firstFooter: company}
        headerFooter:{differentFirst: false, oddHeader: company, evenHeader:company, oddFooter: "第 &P 页，共 &N 页", evenFooter: "第 &P 页，共 &N 页"}
    })
    // sheet1.headerFooter.differentFirst = true;
    // sheet1.headerFooter.firstHeader = company;
    // sheet1.headerFooter.firstFooter = "Hello World"

    // 表头数据添加
    sheet1.columns = columns.map(item => ({
        header: item.title,
        key: item.key,
        width: item.width,
        // height: 20
    }));
    // 表格内容添加
    rows.map(item => sheet1.addRow(item));

    //边框
    for (let num = 0; num < sheet1.lastRow!.number; num++) { // 循环出每一行
        for (let index = 0; index < columns.length; index++) { // 循环出每一个单元格
            sheet1.getRow(num+1).getCell(index+1).border={ // 为单元格添加边框
                top: {style:'thin'},
                left: {style:'thin'},
                bottom: {style:'thin'},
                right: {style:'thin'}
            }
            sheet1.getRow(num+1).getCell(index+1).alignment = { vertical: 'middle', horizontal: 'left'}
            sheet1.getRow(num+1).height = 20 //设置行高
        }
    }

    workbook.xlsx.writeBuffer().then(buffer => {
        saveAs(
            new Blob([buffer], { type: 'application/octet-stream' }),
            // `${name}(${dayjs().format('YYYY年MM月DD日HH时mm分')}导出).xlsx`
            `${name}-${company}-(${dayjs().format('YYYY年MM月DD日HH时mm分')}导出).xlsx`
        );
    });
}
